<?php


namespace app\api\controller;

use app\common\bean\ConfigBean;
use app\common\bean\ListMap;
use app\common\bean\StoreBean;
use app\common\bean\UserBean;
use app\common\controller\ApiController;
use app\common\enum\StoreStatusEnum;
use app\common\manage\StoreManage;
use app\common\model\ConfigModel;
use app\common\model\StoreModel;
use app\common\model\UserModel;
use app\common\utils\ExitJsonUtil;

class Store extends ApiController
{
    protected $beforeActionList = [
        'decryptToken' => ['except' => 'storeList'],
    ];

    public function info(){//获取用户当前门店信息
        $storeBean = new StoreBean();
        $storeBean->setId($this->postParams['store_id']);
        $storeBean = StoreManage::getInstance()->getInfo($storeBean);
        ExitJsonUtil::getInstance()->exitData($storeBean->getData());
    }

    public function applyInfo()
    {//申请详情
        $data['platform_phone'] = ConfigModel::getInstance()->selectData([ConfigBean::$id => 8])[ConfigBean::$value];
        if ($store = StoreModel::getInstance()->selectData([StoreBean::$userId => $this->userId])) {
            $data['contact_name'] = $store[StoreBean::$contactName];
            $data['phone'] = $store[StoreBean::$contactPhone];
            $data['contact_addr'] = $store[StoreBean::$contactAddr];
            $data[StoreBean::$status] = $store[StoreBean::$status];
        } else {
            $data[StoreBean::$status] = StoreStatusEnum::WITHOUT;
        }
        ExitJsonUtil::getInstance()->exitData($data);
    }

    public function apply()
    {//申请加盟
        $storeBean = new StoreBean();
        $storeBean->setData($this->postParams);
        $storeBean->setUserId($this->userId);
        if ($store = StoreModel::getInstance()->selectData([StoreBean::$userId => $this->userId])) {
            ExitJsonUtil::getInstance()->exitData([], 503, '您已申请过门店');
        } else {
            $store_id = StoreManage::getInstance()->add($storeBean);
            ExitJsonUtil::getInstance()->exitData('申请成功');
        }
    }

    public function storeList(){//商家店铺列表
        $listMap = new ListMap();
        $listMap->setData($this->postParams);
        $res = StoreManage::getInstance()->getPaginaList($listMap);
        $data['list'] = $res->getList();
        $data['currentPage'] = $res->getCurrentPage();
        $data['totalCount'] = $res->getTotalCount();
        ExitJsonUtil::getInstance()->exitData($data);
    }
}